import axios from 'axios';
// import * as routerRedux from 'react-router-redux';
import { Modal } from 'antd';
import { getServer } from 'utils/share';
import { server } from 'configs';
import qs from 'qs';

const http = (type) => {
  const axiosRequest = axios.create({
    withCredentials: true,
    baseURL: getServer(server)[type],
    timeout: 5000
  });
  // post 请求
  const post = (url, params) => axiosRequest.post(url, qs.stringify(params), {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }
  })
    .then(({data}) =>  {

      if(data.status === 'ERROR'){
        Modal.error(
          { 
            title: data.msg || '请求失败',
            onOk: ()=> {
              // code=10，未登陆
              if(data.code === 10){
                window.location.hash = '#/login';
              }
            }
          },
        );
      }
      return data;
    })
    .catch(() => { Modal.error({ title: '请求失败' }); return false; });
  return {
    post
  };
};
export default http;

